function Rank(){
	this.baseUrl = 'http://api.shihou.tv/api/gift';
	this.receive_url = this.baseUrl + '/receive_rank?gift_id=';
	this.send_url = this.baseUrl + '/send_rank?gift_id=';
	this.gift_ids = [1092];//1087黑板擦、1088粉笔
	this.user_rank_pre_html = ['MVP榜排名:', '铁粉榜排名:'];
	this.init();
}
Rank.prototype={
	init: function(){
		var _this = this;
	},
	setRankList: function(index, page){
		var _this = this;
		var size = 0;
		var recevie_data_array = [];
		var api_url = _this.receive_url + _this.gift_ids[index];
		_this.requestRemoteDataJson(api_url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					recevie_data_array[i] = user;
					size = i;
					if(i == 9){
						break;
					}
				}
			}
		}, false);
		
		var send_data_array = [];
		api_url = _this.send_url + _this.gift_ids[index];
		_this.requestRemoteDataJson(api_url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					send_data_array[i] = user;
					if(i > size){
						size = i;
					}
					if(i == 9){
						break;
					}
				}
			}
		}, false);
		
		
		for(var j = 0;j <= size;j++){
			var html = '';
			var recive = recevie_data_array[j];
			var send = send_data_array[j];
			var roomSkip = '';
			if(page == 0){
				roomSkip = _this.parseResult(recive, 'user', 'room', 'jump');
			}else{
				roomSkip = 'http://event.m.shihou.tv/lion_module_share_temp/share?room_id='+_this.parseResult(recive, 'user', 'room', 'id');
			}
			html += _this.setRankListHTML((j+1), index, 1, roomSkip, _this.parseResult(recive, 'user', 'nick_name'), _this.parseResult(recive, 'user', 'avatar'), _this.parseResult(recive, 'user', 'room', 'vdo_status'), _this.parseResult(recive, 'total')/6);
			html += _this.setRankListHTML((j+1), index, 2, roomSkip, _this.parseResult(send, 'user', 'nick_name'), _this.parseResult(send, 'user', 'avatar'), _this.parseResult(send, 'user', 'room', 'vdo_status'), _this.parseResult(send, 'total')/6);
			if(j < 3){
				$('#phbList').append(html);
			}else{
				$('#phbList4').append(html);
			}
		}
		
	},
	setUserRank: function(index, uid){
		var _this = this;
		var rankHTML = _this.user_rank_pre_html[index];
		var rank = null;
		if(uid == "0"){
			rankHTML += "未上榜";
			return;
		}
		var api_url = '';
		if(index == 0){
			api_url = _this.receive_url + _this.gift_ids[0];
		}
		if(index == 1){
			api_url = _this.send_url + _this.gift_ids[0];
		}
		_this.requestRemoteDataJson(api_url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					var user_id = _this.parseResult(user, "user", "id");
					if(user_id == uid){
						rank = (i+1);
					}
				}
			}
		}, false);	
		
		if(rank == null)
			rankHTML += "未上榜";
		else
			rankHTML += rank;
		if(index == 0){
			$('#userRank1').html(rankHTML);	
		}
		if(index == 1){
			$('#userRank2').html(rankHTML);	
		}
	},
	setRankListHTML: function(index, num, anchorOrUser, skipRoom, nick_name, avatar, kaibo_status, value){
		var _this = this;
		var html = '';
		var avatarHTML = '';
		var zhiboHTML = '';
		var liveRank = '';
		var liveNo = '';
		
		if(index < 4){
			liveRank = '<img src="static/img/icon'+index+'.png" />';
			liveNo = 'liveNo'+index;
		}else{
			liveRank = index;
			liveNo = 'liveNo4';
		}
		
		if(kaibo_status == 1){//如果开播
			zhiboHTML = '<em class="liveIcon"></em>'+
                        '<em class="liveOpacity"></em>'+
                        '<div class="loader-inner line-scale-pulse-out">'+
                        '    <div></div>'+
                        '    <div></div>'+
                        '    <div></div>'+
                        '    <div></div>'+
                        '    <div></div>'+
                        '</div>';
		}
		
		if(anchorOrUser == 1){
			html = '<li class="anchor">'+
                   '    <a href="'+skipRoom+'" class="'+liveNo+'">'+
                   '        <span class="liveHead">'+
                   '            <span class="liveWrap">'+
                   					zhiboHTML+
                   '                <img src="'+avatar+'"/>'+
                   '            </span>'+
                   '        </span>'+
                   '        <span class="liveName">'+nick_name+'</span>'+
                   '        <span class="liveNum">收到'+value+'个</span>'+
                   '        <span class="liveRanking">'+liveRank+'</span>'+
                   '    </a>'+
                   '</li>';
		}else{
			html = '<li class="user">'+
                   '    <a class="'+liveNo+'">'+
                   '        <span class="liveHead"><img src="'+avatar+'"></span>'+
                   '        <span class="liveName">'+nick_name+'</span>'+
                   '        <span class="liveNum">送出'+value+'个</span>'+
                   '    </a>'+
                   '</li>';
		}
		return html;
	},
	sortarr: function(arr){
		var _this = this;
		for(i=0;i<arr.length-1;i++){
	        for(j=0;j<arr.length-1-i;j++){
	        	if(arr[j] == undefined || arr[j] == 'undefined' || arr[j+1] == undefined || arr[j+1] == 'undefined')
	        		break;
	        	var total1 = arr[j]['total'];
	        	var total2 = arr[j+1]['total'];
	            if(total1 < total2){
	                var temp=arr[j];
	                arr[j]=arr[j+1];
	                arr[j+1]=temp;
	            }
	        }
	    }
	    return arr;
	},
	//获取url中的参数
    getUrlParam: function(name, url) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        url = url || window.location.search.substr(1).match(reg);  //匹配目标参数
        if (url != null) return unescape(url[2]); return null; //返回参数值
    },
    getUrlParams: function(){
    	var url = window.location.href;  //匹配目标参数
    	if(url != null)
    		return url.substr(url.indexOf("?"), url.length);
    },
    parseResult:function(){//解析结果， 如果不存在则返回''
		var size = arguments.length;
		if(size == 0){
			return '';
		}
		var result = arguments[0];
		for(var i = 1; i < size; i++){
			if(result == undefined){
				return '';
			}
			var argu = arguments[i];
			if(i == size){
				return argu;
			}
			result = result[argu];
		}
		return result;
	},
	requestRemoteDataJson: function(url, data, callback, asyn){
		//this.logURL(url);
		//this.logParameter(data);
		$.ajax({
	        url: url,
	        type: 'GET',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	requestRemotePostDataJson: function(url, data, callback, asyn){
		//this.logURL(url);
		$.ajax({
	        url: url,
	        type: 'POST',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	log:function(data){
		console.log(data);
	},
	logURL:function(url){
		console.log("url --> "+url);
	},
	logParameter:function(data){
		console.log("parameter --> "+JSON.stringify(data));
	},
	logParameter:function(args, data){
		console.log(args+" --> "+JSON.stringify(data));
	},
	logResult:function(result){
		console.log("result --> "+JSON.stringify(result));
	}
}


